package finaldic;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class Frame2 extends JFrame{
	
	public Frame2(String title) {
	
		super(title);
		setResizable(false);
		getContentPane().setLayout(null);
		
		JFrame frame=new JFrame("英汉字典");
		JLabel label1=new JLabel("请输入英文：");
		label1.setBounds(20,13,100,30);
		JLabel label2=new JLabel("翻译结果：");
		label2.setBounds(20,100,100,30);
		getContentPane().add(label1);
		getContentPane().add(label2);
		JButton button1=new JButton("翻译");
		button1.setBounds(19,300,100,30);
		getContentPane().add(button1);
		setSize(600,400);
		JButton button2=new JButton("重置");
		button2.setBounds(139,300,100,30);
		getContentPane().add(button2);
		final JTextField text1=new JTextField(12);
		text1.setBounds(120,13,200,50);
		getContentPane().add(text1);
		final JTextField text2=new JTextField(12);
		text2.setBounds(120,100,200,50);
		getContentPane().add(text2);
		setVisible(true);
		
		button1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent event) {
			Dict d = new localdic("words.txt");//生成本地字典
			String word=text1.getText();//读入要查询的单词
			String res=d.translate(word);//用本地字典查询单词
		
			if(res!=null) {//本地查询成功
				text2.setText(res);

			}	else	
    		{//本地查询失败，启动网络查询
			baidudic dd=new baidudic();//把d变成百度网络字典
    		String res1=dd.translate(word);
    		text2.setText(res1);
    	}
			
		}
			
		}	);
		button2.addActionListener(new ActionListener(){

			
			public void actionPerformed(ActionEvent arg0) {
				
				text1.setText(null);
				
			}
			
		});
	}
	}
		
		

